import { defineConfig, UserConfig, loadEnv, Plugin } from 'vite';
import vue from '@vitejs/plugin-vue';
import compression from 'vite-plugin-compression';
import visualizer from 'rollup-plugin-visualizer';

interface CustomEnvVariables {
    VITE_BASE_URL: string;
    VITE_HTTP_MOCK?: 'on' | 'off';
    VITE_SOURCE_MAP?: 'on' | 'off';
    VITE_GZIP?: 'on' | 'off';
    VITE_BROTLI?: 'on' | 'off';

    VITE_API_BASE_URL: string;
    // ... more here and start with 'VITE_'
}

export default defineConfig(({ mode }): UserConfig => {
    const env = loadEnv(mode, __dirname) as unknown as CustomEnvVariables;

    return {
        base: env.VITE_BASE_URL,
        plugins: [
            vue(),

            mode === 'report' &&
                visualizer({
                    filename: './dist/_report.html',
                    open: mode === 'report',
                }),
            env.VITE_GZIP === 'on' &&
                compression({
                    algorithm: 'gzip',
                    ext: '.gz',
                }),
            env.VITE_BROTLI === 'on' &&
                compression({
                    algorithm: 'brotliCompress',
                    ext: '.br',
                }),
        ].filter(Boolean) as Plugin[],
        resolve: {
            alias: {
                '@': '/src',
                // 'vue-i18n': 'vue-i18n/dist/vue-i18n.esm-bundler.js',
            },
        },
        css: {
            preprocessorOptions: {
                less: {
                    modifyVars: {
                        'font-size-base': '12px',
                        'table-padding-vertical': '12px',
                        'btn-height-base': '28px',
                        'input-height-base': '28px',
                    },
                    javascriptEnabled: true,
                },
                scss: {
                    additionalData: `@import "src/styles/global.scss";`,
                },
            },
        },
        optimizeDeps: {
            include: ['moment', 'lodash', 'moment/locale/zh-cn'],
            exclude: [
                // 'xxx'
            ],
        },
        server: {
            // port: 80,
            // proxy: {
            //     '/api': {
            //         target: 'http://10.172.10.88:8000',
            //         changeOrigin: true,
            //         rewrite: (path) => path.replace(/^\/api/, '/'),
            //     },
            // },
            host: '0.0.0.0',
        },
        build: {
            sourcemap: env.VITE_SOURCE_MAP === 'on',
        },
    };
});
